'use strict'

const Controller = require('egg').Controller

class ProjectController extends Controller {
  // 列表
  async index() {
    const { ctx } = this
    const { page = '1', size = '10' } = ctx.query
    const pageSize = Math.max(parseInt(size, 10), 5)
    const pageNumber = Math.max(parseInt(page), 1)

    const total = await ctx.model.Project.find({ members: { $in: [ctx.session.userId] } }).countDocuments()

    // 当前用户自己的或参与的项目
    const list = await ctx.model.Project.find(
      { members: { $in: [ctx.session.userId] } },
      // 排除字段
      {
        members: 0,
        coverUrl: 0,
        status: 0,
        __v: 0,
      }
    )
      .sort({ createdAt: -1 })
      .skip((pageNumber - 1) * pageSize)
      .limit(pageSize)
      .populate({ path: 'owner', select: 'nickname avatar' })

    ctx.body = { code: 2000, data: { list, total, pageNumber, pageSize }, msg: '项目列表查询成功' }
  }

  // 详情
  async show() {
    const { ctx } = this
    const { id } = ctx.params

    try {
      const project = await ctx.model.Project.findById(id, { __v: 0, coverUrl: 0, status: 0 })
        .populate({ path: 'members', select: 'nickname avatar' })
        .populate({ path: 'owner', select: 'nickname avatar' })
      ctx.body = { code: 2000, data: project, msg: '项目查询成功' }
    } catch (err) {
      ctx.body = { code: 4001, data: null, msg: '没有找到项目' }
    }
  }

  // 新建
  async create() {
    const { ctx } = this
    const { members = [], title = '', desc = '', bgColor = '', coverUrl = '' } = ctx.request.body
    const owner = ctx.session.userId
    const res = await ctx.model.Project.create({ owner, members, title, desc, bgColor, coverUrl })

    ctx.body = { code: 2000, data: res._id, msg: '创建项目成功' }
  }

  // 更新
  async update() {
    const { ctx } = this
    const { id } = ctx.params
    const updateFields = ctx.request.body

    let project = null
    try {
      project = await ctx.model.Project.findById(id)
    } catch (err) {
      ctx.body = { code: 4001, data: null, msg: '没有找到项目' }
      return
    }

    if (String(project.owner) !== String(ctx.session.userId)) {
      ctx.body = { code: 4002, data: null, msg: '没有权限修改当前项目' }
      return
    }

    const updatedAt = Date.now()
    await ctx.model.Project.findById(id).update({ ...updateFields, updatedAt })

    ctx.body = { code: 2000, data: null, msg: '项目更新成功' }
  }

  // 删除
  async destroy() {
    const { ctx } = this
    const { id } = ctx.params

    let project = null
    try {
      project = await ctx.model.Project.findById(id)
    } catch (err) {
      ctx.body = { code: 4001, data: null, msg: '没有找到项目' }
      return
    }

    if (String(project.owner) !== String(ctx.session.userId)) {
      ctx.body = { code: 4002, data: null, msg: '没有权限修改当前项目' }
      return
    }

    // 删除项目
    await ctx.model.Project.findById(id).remove()
    // 删除项目中所有用户的任务
    await ctx.model.Todo.find({ pid: id }).remove()
    ctx.body = { code: 2000, data: null, msg: '项目删除成功' }
  }
}

module.exports = ProjectController
